import { defineStore } from 'pinia'
//1、定义并导出容器
//参数1：容器的id，必须唯一
//参数2：选项对象
export const useShopStore = defineStore('shop', {
    /*
    类似于组件的data，用来存储全局状态（必须是箭头函数
    */

    state: () => {
        return {
            shopList: [],
            isAllCheck: false,
        }

    },

    getters: {
        money() {
            return this.shopList.filter(item => item.checked).reduce((total, item) => total += (item.count || 1) * item.price, 0)
        },
        isHidden() {
            if (this.shopList.length > 0) {
                return true
            } else {
                return false
            }
        },
    },
    actions: {
        addNum(i) {
            this.shopList[i].count++;
        },
        subNum(i) {
            this.shopList[i].count--;
            if (this.shopList[i].count <= 1) this.shopList[i].count = 1
        },
        changeItemState(i) {
            this.shopList[i].checked = !this.shopList[i].checked
        },
        //删除
        removeItems() {
            this.shopList = this.shopList.filter(item => item.checked !== true)

        },
        //
        config() {
            this.shopList = this.shopList.filter(item => {
                return item.checked !== true
            })
        }
    },
    persist: true,
})